package com.andiary.common.note;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.andiary.common.R;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;



public class Note extends Activity {
	
	private ImageView mImage;
	private TextView mdayDate;
	private EditText mTitleText;
    private EditText mBodyText;

    
    private int IMAGE_SEARCH=0;
    private int SMS_SEARCH=1;
    private int MAIL_SEARCH=2;
    
    Calendar dateAndTime=Calendar.getInstance();
    DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
    
    Calendar date=GregorianCalendar.getInstance();
    DateFormat fmtDate=DateFormat.getDateInstance();
     
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_edit);
        
        mdayDate=(TextView)findViewById(R.id.daydate);
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        mImage= (ImageView) findViewById (R.id.image);
      
        
        ImageButton btnDay = (ImageButton) findViewById (R.id.event1);
        ImageButton btnInfo = (ImageButton) findViewById (R.id.info);
        Button btnSelectImage = (Button) findViewById (R.id.search_image);
        Button btnConfirmButton = (Button) findViewById(R.id.confirm);
        Button btnSendMail= (Button) findViewById (R.id.mail);
        Button btnClearAll= (Button) findViewById (R.id.clear);
        Button btnSendSMS = (Button) findViewById (R.id.SMS);
      
    
    populateFields();
	
	btnDay.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {

    	}
      
    });
	
	btnInfo.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {
    		//-----------------------------------------------------------
    		/** go to info activity */
        	//Intent info = new Intent(Note.Info.class);
    	    //startActivity(info);
    		//-------------------------------------------------------------
      	   
    	}
      
    });
	
	btnSelectImage.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {
    		
    	try{	
    		
            Intent image = new Intent(Note.this,SelectImage.class);	
            startActivityForResult(image,IMAGE_SEARCH);
  		
			}catch  (Throwable t) {
				
			}
    	}
    });
	
		
    btnConfirmButton.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {
    	    setResult(RESULT_OK);
    	    finish();
    	}
      
    });
    
    btnSendMail.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {
    		
        	try{	
        		
                Intent image = new Intent(Note.this,SendMail.class);	
                startActivityForResult(image,MAIL_SEARCH);
      		
    			}catch  (Throwable t) {
    				
    			}
    		
    	}
      
    });
    
    btnSendSMS.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {
    		
        	try{	
        		
                Intent image = new Intent(Note.this,SendSMS.class);	
                startActivityForResult(image,SMS_SEARCH);
      		
    			}catch  (Throwable t) {
    				
    			}
    		
    	}
      
    });
    
    btnClearAll.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View view) {

    	}
      
    });
}

	private void populateFields() {
		//for the future code, there will have a test if the row of the databse is null...
		//if(true){
		//retrieve data from the Database
            mdayDate.setText(fmtDate.format(date.getTime()));
          //titleCursor: Cursor for all ID and TITLE in the media
          
            //Cursor	idCursor = managedQuery(Media.EXTERNAL_CONTENT_URI, new String[] {
				//		Media._ID}, null, null, Media.DATE_ADDED);
	     // if (idCursor.moveToFirst()) {
		     //For all TITLE
		
			//IF for that TITLE  its ID is equal a the ID of selected image 
			//int mIdColumnIndex = idCursor.getColumnIndex(Media._ID);
			//String idofimage=idCursor.getString(mIdColumnIndex);
        
       // Uri uriImage=Media.EXTERNAL_CONTENT_URI;
		//uriImage=Uri.withAppendedPath(uriImage,idofimage);
        
         //try { 
        	// mImage.setImageURI(uriImage);
        	 //mImage.setAdjustViewBounds(true);
  
         //}catch (Exception e) {
           //    Log.e("No good", uriImage.toString(), e);
             //   }
         
	      //}
           

        //} else {
        	updateLabel();
        	//}
	}
	@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    private void saveState() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        String daydate = mdayDate.getText().toString();
        //String firstaccessnote= (fmtDateAndTime.format(dateAndTime.getTime())).toString(); 
        //String lastaccessnote= (fmtDateAndTime.format(dateAndTime.getTime())).toString();  
    
    }
    private void updateLabel() { 
       //String firstAccessNote= (fmtDateAndTime.format(dateAndTime.getTime())).toString(); 
        mdayDate.setText(fmtDate.format(date.getTime())); 
      } 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode==IMAGE_SEARCH) { 
        	populateFields();
        }
    } 
}

		
	